package third.lib.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class DateTimeUtils {
    /**
     * 获取当前毫秒
     * @return
     */
    public static long getMillis(){
        return Instant.now().toEpochMilli();
    }

    /**
     * 获取当前秒
     * @return
     */
    public static long getSeconds(){
        return Instant.now().getEpochSecond();
    }

    /**
     * 获取当前时间戳 format格式
     * @param format
     * @return
     */
    public static String getCurTimeFormart(String format) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return dtf.format(LocalDateTime.now());
    }

    /**
     * 把时间戳转化为format格式
     * @param ts
     * @param format
     * @return
     */
    public static String convertTimeSpanToTimeFormat(long ts, String format){
        Instant ins = Instant.ofEpochSecond(ts);
        ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault());
        return zdt.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 获取当日0点时间戳
     * @return
     */
    public static long getCurDayLong() {
        LocalDateTime ldt = LocalDate.now().atTime(0, 0, 0);
        ZonedDateTime zbj = ldt.atZone(ZoneId.systemDefault());
        return zbj.toInstant().getEpochSecond();
    }

    /**
     * 获取当月0点时间戳
     * @return
     */
    public static long getCurMonthLong() {
        LocalDateTime ldt = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
        ZonedDateTime zbj = ldt.atZone(ZoneId.systemDefault());
        return zbj.toInstant().getEpochSecond();
    }
}
